package com.wx.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.common.utils.ImagePathUtil;
import com.wx.mapper.SourceDetailMapper;
import com.wx.pojo.Source;
import com.wx.pojo.SourceDetail;
import com.wx.service.SourceDetailService;
/**
 *商品详细信息 
 */
@Service
public class SourceDetailServiceImpl implements SourceDetailService {

	@Autowired
	private SourceDetailMapper sourceDetailMapper;
	/**
	 * 查询商品详细
	 */
	@Override
	public SourceDetail findSourceById(Integer sourceId) {
		Source source=sourceDetailMapper.findSourceById(sourceId);
		SourceDetail sourceDetail=new SourceDetail();
		if(source!=null) {
			sourceDetail.setSourceId(source.getSourceId());
			sourceDetail.setSource_name(source.getSource_name());
			sourceDetail.setSource_count(source.getSource_count());
			
			if(source.getSource_image()!=null) {
				String[] source_image=ImagePathUtil.sourceImagePath(source.getSource_image());
				sourceDetail.setSource_image(source_image);
			}
			
			sourceDetail.setSource_price(source.getSource_price());
			sourceDetail.setSource_deposit(source.getSource_deposit());
			sourceDetail.setSource_text(source.getSource_text());
			if(source.getProvince()!=null && source.getCity()!=null && source.getCounty()!=null ) {
				String detail_address=source.getProvince()
								+source.getCity()+source.getCounty()
								+source.getSource_address();
				sourceDetail.setDetail_address(detail_address);
			}
		}
		return sourceDetail;
	}

	/**
	 * 展示自己发布的信息
	 */
	@Override
	public List<SourceDetail> findPublishSourceByUserId(String userId) {
		List<SourceDetail> sourceDetailList=new ArrayList<>();
		List<Source> sourceList =sourceDetailMapper.findPublishSourceByUserId(userId);
		for (Source source : sourceList) {
			SourceDetail sourceDetail = new SourceDetail();
			sourceDetail.setSourceId(source.getSourceId());
			sourceDetail.setSource_name(source.getSource_name());
			sourceDetail.setSource_price(source.getSource_price());
			sourceDetail.setSource_deposit(source.getSource_deposit());
			sourceDetail.setSource_count(source.getSource_count());
			if(source.getSource_image()!=null) {
				String[] source_image=ImagePathUtil.sourceImagePath(source.getSource_image());
				sourceDetail.setSource_image(source_image);
			}
			sourceDetail.setSource_text(source.getSource_text());
			//拼接详细地址
			sourceDetail.setDetail_address(source.getProvince()
					+source.getCity()+source.getCounty()
					+source.getSource_address());
			sourceDetailList.add(sourceDetail);//添加进数组
		}
		
		return sourceDetailList;
	}

}
